package com.it.park.controller.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.park.client.service.ArticleCollectService;
import com.it.park.client.service.ArticleService;
import com.it.park.client.service.ArticleTypeService;
import com.it.park.client.service.SensitiveWordService;
import com.it.park.common.JsonObj;
import com.it.park.common.Page;
import com.it.park.common.SessionUtil;
import com.it.park.constant.Constant;
import com.it.park.model.Article;
import com.it.park.model.ArticleCollect;
import com.it.park.model.ArticleType;
import com.it.park.model.SensitiveWord;
import com.it.park.rms.client.model.User;

@Controller
@RequestMapping("/user/article")
public class UserArticleController {
	private String rootPath=Constant.USER_PATH;
	@Autowired
	private ArticleService articleService;
	@Autowired
	private ArticleTypeService articleTypeService;
	@Autowired
	private ArticleCollectService collectService;
	@Autowired
	private SensitiveWordService sensitiveWordService;
	private Page page = null;
	
	@RequestMapping("articleManage")
	public String articleManage(Integer pageNow,HttpServletRequest request,HttpSession session){
			List<Article> articles =	null;
			User user = (User) session.getAttribute("user");
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("userId", user.getId());
			Integer totals = articleService.selectTotal(map);
			page = new Page(pageNow,totals,10);
			map.put("startPos", page.getStartPos());
			map.put("pageSize", page.getPageSize());
			articles = articleService.listRelationUser(map);
			request.setAttribute("totals", totals);
			request.setAttribute("page", page);
			request.setAttribute("articles", articles);
			
			//我的收藏
			List<ArticleCollect> collects =  collectService.list(user.getId());//我的收藏
			request.setAttribute("collects", collects);
			
			//显示回收站中的文章
			List<Article> listRecycleArticles = articleService.selectFromRecycle(user.getId());
			request.setAttribute("listRecycleArticles", listRecycleArticles);
			
			//显示回草稿箱中的文章
			List<Article> listDraftArticles = articleService.selectFromDraft(user.getId());
			request.setAttribute("listDraftArticles", listDraftArticles);
			
			return rootPath+"article/articleManage";
	}
	
	/**
	 *单个文章详情 
	 */
	@RequestMapping("detail")
	public String detail(@RequestParam("id") Integer id,HttpServletRequest request){
		articleService.updateClickRate(id);//每点击一次，加1
		Article article = articleService.selectByPrimaryKey(id);
		request.setAttribute("article", article);
		return rootPath+"article/articleDetail";
	}
	
	
	/**
	 *发表文章页面
	 */
	@RequestMapping("publishArticle")
	public String publishArticle(HttpServletRequest request){
		List<ArticleType> articleTypes = articleTypeService.selectAll();
		request.setAttribute("articleTypes", articleTypes);
		return rootPath+"article/publishArticle";
	}
	
	/**
	 *
	 *根据大的分类查询
	 */
	@RequestMapping("selectByCategory")
	@ResponseBody
	public List<ArticleType> selectByCategory(HttpServletRequest request){
		String category = request.getParameter("category");
		if(category == null || category.length() <=0)
			category = "1";
		List<ArticleType> articleTypes = articleTypeService.selectByCategory(Integer.parseInt(category));
		return articleTypes;
	}
	
	/**
	 *发布文章
	 */
	@RequestMapping("publishArticleSave")
	@ResponseBody
	public JsonObj publishArticleSave(@RequestParam("articleTypeId")Integer articleTypeId,Article article,HttpServletRequest request,HttpSession session){
			JsonObj json = new JsonObj();
			User user = (User) session.getAttribute("user");
			ArticleType articleType = articleTypeService.selectByPrimaryKey(articleTypeId);
			article.setUser(user);
			article.setArticleType(articleType);
			article.setCreateDate(new Date());
			//过滤敏感词
			String articleContent = article.getArticleContent();
			List<SensitiveWord> words = sensitiveWordService.selectAll();
			for (SensitiveWord word : words) {
				articleContent = articleContent.replaceAll( word.getWord(), "***");
			}
			article.setArticleContent(articleContent);
			boolean success = articleService.insertSelective(article);
			if(success){
				json.setMessage("保存成功,等待管理员审核!");
			}else{
				json.setMessage("保存失败!");
			}
			json.setSuccess(true);
			json.setObject(article);
			return json;
	}
	
	/**
	 * 删除到回收站
	 */
	@RequestMapping("putToRecycle")
	public String putToRecycle(Integer id){
		articleService.putToRecycle(id);
		return "redirect:articleManage";
	}
	
	/**
	 *设置为草稿
	 */
	@RequestMapping("setToDraft")
	public String setToDraft(Integer id){
		 articleService.setToDraft(id);
		return "redirect:articleManage";
	}
	
	/**
	 *草稿页面
	 */
	@RequestMapping("articleDraft")
	public String articleDraft(HttpServletRequest request,HttpSession session){
		//显示回草稿箱中的文章
		List<Article> listDraftArticles = articleService.selectFromDraft(SessionUtil.getUserID(session));
		request.setAttribute("listDraftArticles", listDraftArticles);
		 return rootPath+"article/articleDraft";
	}
	
	/**
	 *回收站页面
	 */
	@RequestMapping("articleRecycle")
	public String articleRecycle(HttpServletRequest request,HttpSession session){
		//显示回收站中的文章
		List<Article> listRecycleArticles = articleService.selectFromRecycle(SessionUtil.getUserID(session));
		request.setAttribute("listRecycleArticles", listRecycleArticles);
		return rootPath+"article/articleRecycle";
	}
	
	/**
	 * 彻底删除
	 */
	@RequestMapping("deleteArticle")
	public JsonObj deleteArticle(Integer id){
		JsonObj jsonObj = new JsonObj();
		boolean success =  articleService.deleteByPrimaryKey(id);
		if(success){
			jsonObj.setMessage("删除成功!");
		}else{
			jsonObj.setMessage("删除失败!");
			jsonObj.setSuccess(false);
		}
		return jsonObj;
	}
	
	/**
	 * 从回收站恢复
	 */
	@RequestMapping("recoverFromRecycle")
	public JsonObj recoverFromRecycle(Integer id){
		JsonObj jsonObj = new JsonObj();
		boolean success =  articleService.recoverFromRecycle(id);
		if(success){
			jsonObj.setMessage("恢复成功!");
		}else{
			jsonObj.setMessage("恢复失败!");
			jsonObj.setSuccess(false);
		}
		return jsonObj;
	}
	
}
